import PDrawer from '../../../components/Pro/PDrawer';
import React from 'react';
import {
  Form,
  Input,
  Button,
  DatePicker,
  Popover,
  Icon,
  Select
} from 'antd';
import {formItemLayout1} from './constants';
import {DrawHeader, Empty, DrawBottomBtns, DrawTitle, ImageDontDelete} from './styles';
import {OrganName} from './components';
import PCascader from '../../../components/Pro/PCascader';
import PFromUpload from '../../../components/Pro/PFromUpload';
import {
  getComBaseInfoById,
  publicCommonDictGetDictByCodeTypes,
  enterpriseCompanyGetComIndustryinfo
} from '../../../services/api';

const Option = Select.Option;

const FormItem = Form.Item;
@Form.create()
export default class DrawChildCompanyIdentification extends React.Component {
  constructor(props, context) {
    super(props, context);
    this.state = {
      dataSource: {
        legalRepName: "",//法定代表人
        nationality: "",//国籍/地区
        legalRepPhone: "",//手机号
        idCardType: "",//证件类型
        idCardNo: "",//证件号
        regAmount: "",//注册资金（万元）
        fixedAssets: "",//固定资产
        annualTurnover: "",//年营业额
        annualProfitTax: "",//年利率
        comCerType: "",//企业证件类型
        comCerNo: "",//证件号
        comCerUrl: "",// 营业执照
      },
      codeTypes: {
        nationality: [],//国籍
        idCardType_bak: [],//证件类型
        conf_association: [],//所属行业保安协会
        conf_safeRange: [],//安全范围
        conf_manpowerLevel: [],//人力防范等级
        conf_armedLevel: [],//武装押运等级
        conf_safeTechLevel: [],//安全技术防范等级
        conf_riskLevel: [],//风险评估等级
        comCerType: [],//企业证件类型
      },
      industryInfo: [],// 行业资质
    }
  }

  /******************************生命周期******************************/

  componentDidMount = () => {
    this.publicCommonDictGetDictByCodeTypes();

  }
  componentWillReceiveProps = (nextProps, newState) => {
    console.log(nextProps.visible && !this.props.visible && !!nextProps.comId);
    console.log(this.props.visible);

    if (nextProps.visible && !this.props.visible && nextProps.comId) {
      this.ajaxDetail(nextProps.comId);
      this.enterpriseCompanyGetComIndustryinfo(nextProps.comId);
    }
  }

  /******************************ajax请求******************************/

  ajaxDetail = (comId) => {
    getComBaseInfoById({comId})
      .then(
        (data) => {
          console.log("子公司基础信息", data);
          const dataSource = this.state.dataSource;
          this.setState({dataSource: {...dataSource, ...data.data}});
        }
      )
      .catch(
        (err) => {
          console.log(err);
        }
      );
  }

  //类型查询
  publicCommonDictGetDictByCodeTypes = () => {
    const codeTypes = "nationality,idCardType_bak,conf_association,conf_safeRange,conf_manpowerLevel,conf_armedLevel,conf_safeTechLevel,conf_riskLevel,comCerType";
    publicCommonDictGetDictByCodeTypes({codeTypes}).then(({data = {}}) => {
      console.log("类型查询", data);
      this.setState({codeTypes: data});
    }, (err) => {
      console.log(err);
    });
  }

  //企业信息-认证申请-行业资质查询
  enterpriseCompanyGetComIndustryinfo = (comId) => {
    enterpriseCompanyGetComIndustryinfo({comId}).then(
      ({data}) => {
        console.log("industryInfo", data);
        if (!data || data.length === 0) {
          return;
        }
        this.setState({
          industryInfo: data
        });
        console.log(data);
      },
      (err) => {
        console.log(err);
      }
    );
  }

  /******************************相关事件******************************/
  handleSubmit = (e) => {
    e.preventDefault();
    this.props.form.validateFields((err, values) => {
      if (!err) {

      }
    });
  }

  closeDrawer = () => {
    this.props.closeDrawer();
  }


  /******************************render******************************/
  renderOption = (options) => {
    if (options.length === 0) {
      return;
    }
    return options.map(
      (val, key) => (
        <Option
          value={val.codeValue}
          key={key}>
          {val.label}
        </Option>
      ))
  }

  render() {
    const {visible} = this.props;
    const {getFieldDecorator} = this.props.form;
    const {
      legalRepName,
      nationality,
      legalRepPhone,
      idCardType,
      idCardNo,
      regAmount,
      fixedAssets,
      annualTurnover,
      annualProfitTax,
      comCerType,
      comCerNo,
      comCerUrl

    } = this.state.dataSource;

    const codeTypes = this.state.codeTypes;
    const {industryInfo} = this.state;

    //保安服务许可证号
    const findsecurityLicenseNum = () => {
      const result = industryInfo.find(val => val.fieldCode.trim() === "conf_securityLicenseNum");
      if (!result) {
        return "";
      } else {
        return result.fieldValue;
      }
    }
    //安保范围
    const findSelectValue = (name) => {
      const result = industryInfo.find(val => val.fieldCode.trim() === name);
      if (!result) {
        return [];
      }
      if (result.fieldValue == "") {
        return [];
      }
      return result.fieldValue.split(',');
    }
    //上传保安服务许可证
    const findSecurityLicenseUrl = () => {
      const result = industryInfo.find(val => val.fieldCode.trim() === "conf_securityLicenseUrl");
      if (!result) {
        return "";
      } else {
        return result.fieldValue;
      }
    }


    return (
      <PDrawer
        title="子公司认证"
        visible={visible}
        okText={null}
        cancelText="返回"
        onCancel={this.closeDrawer}
        destroyOnClose={true}
      >
        <div>
          <Form onSubmit={this.handleSubmit} style={{width: 390, margin: "6px 0 81px 0"}}>
            <DrawTitle>法定代表人/负责人</DrawTitle>
            <FormItem
              {...formItemLayout1}
              label="法定代表人/负责人姓名"
              style={{marginTop: 24}}
            >
              <Input style={{width: 294}} disabled={true} value={legalRepName}/>
            </FormItem>
            <FormItem
              {...formItemLayout1}
              label="国籍/地区"
              style={{marginTop: 24}}
            >
              <Select disabled={true} value={nationality}>
                {this.renderOption(codeTypes.nationality)}
              </Select>
            </FormItem>
            <FormItem
              {...formItemLayout1}
              label="手机号"
              style={{marginTop: 24}}
            >
              <Input style={{width: 294}} disabled={true} value={legalRepPhone}/>

            </FormItem>
            <FormItem
              {...formItemLayout1}
              label="证件类型"
              style={{marginTop: 24}}
            >
              <Select disabled={true} value={idCardType}>
                {this.renderOption(codeTypes.idCardType_bak)}
              </Select>
            </FormItem>
            <FormItem
              {...formItemLayout1}
              label="证件号"
              style={{marginTop: 24}}
            >
              <Input style={{width: 294}} disabled={true} value={idCardNo}/>
            </FormItem>
            <DrawTitle>
              注册信息
            </DrawTitle>
            <FormItem
              {...formItemLayout1}
              label="注册资金（万元）"
              style={{marginTop: 24}}
            >
              <Input style={{width: 294}} disabled={true} value={regAmount}/>
            </FormItem>
            <FormItem
              {...formItemLayout1}
              label="固定资产（万元）"
              style={{marginTop: 24}}
            >
              <Input style={{width: 294}} disabled={true} value={fixedAssets}/>
            </FormItem>
            <FormItem
              {...formItemLayout1}
              label="年营业额（万元）"
              style={{marginTop: 24}}
            >
              <Input style={{width: 294}} disabled={true} value={annualTurnover}/>
            </FormItem>
            <FormItem
              {...formItemLayout1}
              label="年利率（万元）"
              style={{marginTop: 24}}
            >
              <Input style={{width: 294}} disabled={true} value={annualProfitTax}/>
            </FormItem>
            <FormItem
              {...formItemLayout1}
              label="证件类型"
              style={{marginTop: 24}}
            >
              <Select disabled={true} value={comCerType}>
                {this.renderOption(codeTypes.comCerType)}
              </Select>
            </FormItem>
            <FormItem
              {...formItemLayout1}
              label="证件号"
              style={{marginTop: 24}}
            >
              <Input style={{width: 294}} disabled={true} value={comCerNo}/>

            </FormItem>
            <FormItem
              {...formItemLayout1}
              label="营业执照"
              style={{marginTop: 24}}
            >
              <ImageDontDelete>
                <PFromUpload num={2} value={comCerUrl} disabled={true}/>
              </ImageDontDelete>
            </FormItem>
            <DrawTitle>
              特殊资质信息
            </DrawTitle>

            <FormItem
              {...formItemLayout1}
              label="所属行业保安协会"
              style={{marginTop: 24}}
            >
              <Select disabled={true} value={findSelectValue("conf_association")}>
                {this.renderOption(codeTypes.conf_association)}
              </Select>
            </FormItem>
            <FormItem
              {...formItemLayout1}
              label="安保范围"
              style={{marginTop: 24}}
            >
              <Select disabled={true} value={findSelectValue("conf_safeRange")}>
                {this.renderOption(codeTypes.conf_safeRange)}
              </Select>
            </FormItem>
            <FormItem
              {...formItemLayout1}
              label="人力防范等级"
              style={{marginTop: 24}}
            >
              <Select disabled={true} value={findSelectValue("conf_manpowerLevel")}>
                {this.renderOption(codeTypes.conf_manpowerLevel)}
              </Select>

            </FormItem>
            <FormItem
              {...formItemLayout1}
              label="武装押运等级"
              style={{marginTop: 24}}
            >
              <Select disabled={true} value={findSelectValue("conf_armedLevel")}>
                {this.renderOption(codeTypes.conf_armedLevel)}
              </Select>
            </FormItem>
            <FormItem
              {...formItemLayout1}
              label="安全技术防范等级"
              style={{marginTop: 24}}
            >
              <Select disabled={true} value={findSelectValue("conf_safeTechLevel")}>
                {this.renderOption(codeTypes.conf_safeTechLevel)}
              </Select>

            </FormItem>
            <FormItem
              {...formItemLayout1}
              label="保安服务许可证号"
              style={{marginTop: 24}}
            >
              <Input style={{width: 294}} disabled={true} value={findsecurityLicenseNum()}/>

            </FormItem>
            <FormItem
              {...formItemLayout1}
              label="保安服务许可证"
              style={{marginTop: 24}}
            >
              <ImageDontDelete>
                <PFromUpload num={2} disabled={true} value={findSecurityLicenseUrl()}/>
              </ImageDontDelete>
            </FormItem>
          </Form>
        </div>
      </PDrawer>
    );
  }
}
